#!/usr/bin/python
import sys,os
import json

global characters

#enter custom chars if you want to add them and they doesnt exists in other lists

lower = 'abcdefghijklmnopqrstuvwxyz'
upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
numeric = '0123456789'
symbols = ':!@#$%^&* ()_+~;`\"\'.,?\\|/<>'
custom_chars = ""

characters = lower + upper + numeric + symbols + custom_chars

global keychain
global decode_result

decode_result = ""
keychain = dict()

def decode_cipher(cipher):
	global decode_result
	plaintext = ""
	current_dict = keychain

	for char in cipher:
		char = ord(char)
		if char in current_dict.keys():
			current_dict = current_dict[char]
			plaintext += current_dict['val']
			
		else:
			#create a list of known plaintexts
			solved_keys = list()
			for key in current_dict.keys():
				if(key == 'val'): continue
				solved_keys.append(current_dict[key]['val'])
			
			for ch in characters:
				if(ch not in solved_keys):
					decode_result = plaintext + ch
					return False

			return False
			
	decode_result = plaintext
	return True
		
		
def add_cipher(cipher,plaintext):
	current_dict = keychain
	
	i = 0
	for char in cipher:
		char = ord(char)

		if char not in current_dict.keys():
			current_dict[char] = dict()
			current_dict[char]['val'] = str(plaintext[i])
		current_dict = current_dict[char]
		i += 1

